﻿namespace Sidvall.LogManager.UI.Entities
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling")]
    public sealed partial class LogContext : global::Sidvall.LogManager.UI.Entities.Core.BusinessBase, global::Sidvall.LogManager.Data.ILogContext
    {
        #region Business Members

        #region Category

        private string _Category = string.Empty;
        partial void OnBeforeCategoryPropertyGet(Sidvall.Business.PropertyEventData<string> eventData);
        partial void OnBeforeCategoryPropertyChanged(Sidvall.Business.PropertyChangeEventData<string> eventData);
        partial void OnAfterCategoryPropertyChanged();
        public string Category
        {
            get
            {
                Sidvall.Business.PropertyEventData<string> eventData = new Sidvall.Business.PropertyEventData<string>(_Category);
                OnBeforeCategoryPropertyGet(eventData);
                return eventData.Value;
            }
            set
            {
                Sidvall.Business.PropertyChangeEventData<string> eventData = new Sidvall.Business.PropertyChangeEventData<string>(value, _Category);
                OnBeforeCategoryPropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("Category", eventData.Value, true, true, true);
                    OnAfterCategoryPropertyChanged();
                }
            }
        }

        #endregion
        #region Content

        private string _Content = string.Empty;
        partial void OnBeforeContentPropertyGet(Sidvall.Business.PropertyEventData<string> eventData);
        partial void OnBeforeContentPropertyChanged(Sidvall.Business.PropertyChangeEventData<string> eventData);
        partial void OnAfterContentPropertyChanged();
        public string Content
        {
            get
            {
                Sidvall.Business.PropertyEventData<string> eventData = new Sidvall.Business.PropertyEventData<string>(_Content);
                OnBeforeContentPropertyGet(eventData);
                return eventData.Value;
            }
            set
            {
                Sidvall.Business.PropertyChangeEventData<string> eventData = new Sidvall.Business.PropertyChangeEventData<string>(value, _Content);
                OnBeforeContentPropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("Content", eventData.Value, true, true, true);
                    OnAfterContentPropertyChanged();
                }
            }
        }

        #endregion
        #region EventTypeId

        private System.Nullable<global::Sidvall.Logging.EventType> _EventTypeId = null;
        partial void OnBeforeEventTypeIdPropertyGet(Sidvall.Business.PropertyEventData<System.Nullable<global::Sidvall.Logging.EventType>> eventData);
        partial void OnBeforeEventTypeIdPropertyChanged(Sidvall.Business.PropertyChangeEventData<System.Nullable<global::Sidvall.Logging.EventType>> eventData);
        partial void OnAfterEventTypeIdPropertyChanged();
        public System.Nullable<global::Sidvall.Logging.EventType> EventTypeId
        {
            get
            {
                Sidvall.Business.PropertyEventData<System.Nullable<global::Sidvall.Logging.EventType>> eventData = new Sidvall.Business.PropertyEventData<System.Nullable<global::Sidvall.Logging.EventType>>(_EventTypeId);
                OnBeforeEventTypeIdPropertyGet(eventData);
                return eventData.Value;
            }
            set
            {
                Sidvall.Business.PropertyChangeEventData<System.Nullable<global::Sidvall.Logging.EventType>> eventData = new Sidvall.Business.PropertyChangeEventData<System.Nullable<global::Sidvall.Logging.EventType>>(value, _EventTypeId);
                OnBeforeEventTypeIdPropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("EventTypeId", eventData.Value, true, true, true);
                    OnAfterEventTypeIdPropertyChanged();
                }
            }
        }

        #endregion
        #region LogContextId

        private System.Nullable<int> _LogContextId = null;
        partial void OnBeforeLogContextIdPropertyGet(Sidvall.Business.PropertyEventData<System.Nullable<int>> eventData);
        partial void OnBeforeLogContextIdPropertyChanged(Sidvall.Business.PropertyChangeEventData<System.Nullable<int>> eventData);
        partial void OnAfterLogContextIdPropertyChanged();
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        public System.Nullable<int> LogContextId
        {
            get
            {
                Sidvall.Business.PropertyEventData<System.Nullable<int>> eventData = new Sidvall.Business.PropertyEventData<System.Nullable<int>>(_LogContextId);
                OnBeforeLogContextIdPropertyGet(eventData);
                return eventData.Value;
            }
            internal set
            {
                Sidvall.Business.PropertyChangeEventData<System.Nullable<int>> eventData = new Sidvall.Business.PropertyChangeEventData<System.Nullable<int>>(value, _LogContextId);
                OnBeforeLogContextIdPropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("LogContextId", eventData.Value, true, true, true);
                    OnAfterLogContextIdPropertyChanged();
                }
            }
        }

        #endregion
        #region DataRowContextStorageKey

        protected override string DataRowContextStorageKey
        {
            get
            {
                return global::Sidvall.LogManager.UI.SystemContextConfig.Current.EntityFacade.LogContextManager.GetKey(this);
            }
        }

        #endregion
        #region LoadInstanceField

        public void LoadInstanceField(string fieldName, object value, bool cascade, bool raisePropertyChanged, bool updateState)
        {
            global::Sidvall.LogManager.UI.SystemContextConfig.Current.EntityFacade.LogContextManager.LoadInstanceField(this, fieldName, value, cascade, raisePropertyChanged, updateState);
        }

        #endregion
        #region AcceptChanges

        public void AcceptChanges()
        {
            global::Sidvall.LogManager.UI.SystemContextConfig.Current.EntityFacade.LogContextManager.AcceptChanges(this);
        }

        #endregion
        #region Clone

        public LogContext Clone()
        {
            var copySettings = Sidvall.Data.CopySettings.Clone();
            return Copy(copySettings);
        }

        #endregion
        #region Copy

        public LogContext Copy()
        {
            return Copy(null);
        }
        public LogContext Copy(Sidvall.Data.CopySettings copySettings)
        {
            if (copySettings == null)
                copySettings = new Sidvall.Data.CopySettings();
            if (!Sidvall.Data.DataRowContextManager.IsValid(this, copySettings.RowFilter))
                return null;
            var item = new LogContext();
            Import(item, this, copySettings);
            return item;
        }

        #endregion
        #region Import

        public void Import(global::Sidvall.LogManager.Data.ILogContext item)
        {
            Import(item, null);
        }
        public void Import(global::Sidvall.LogManager.Data.ILogContext item, Sidvall.Data.CopySettings copySettings)
        {
            Import(this, item, copySettings);
        }
        partial void OnAfterImport(LogContext destination, global::Sidvall.LogManager.Data.ILogContext source, Sidvall.Data.CopySettings copySettings);
        private void Import(LogContext destination, global::Sidvall.LogManager.Data.ILogContext source, Sidvall.Data.CopySettings copySettings)
        {
            global::Sidvall.LogManager.UI.SystemContextConfig.Current.EntityFacade.LogContextManager.Import(destination, source, copySettings);
            OnAfterImport(destination, source, copySettings);
        }
        partial void OnAfterImport(LogContext destination, LogContext source, Sidvall.Data.CopySettings copySettings);
        private void Import(LogContext destination, LogContext source, Sidvall.Data.CopySettings copySettings)
        {
            Import(destination, source as global::Sidvall.LogManager.Data.ILogContext, copySettings);
            OnAfterImport(destination, source, copySettings);
        }

        #endregion
        #region ValidateAsync

        public async System.Threading.Tasks.Task<Sidvall.Data.BusinessRuleCollection> ValidateAsync()
        {
            return await ValidateAsync((Sidvall.Data.ValidationSettings)null);
        }
        public async System.Threading.Tasks.Task<Sidvall.Data.BusinessRuleCollection> ValidateAsync(Sidvall.Data.ValidationSettings settings)
        {
            var context = new Sidvall.Business.ValidationContext(settings);
            await global::Sidvall.LogManager.UI.SystemContextConfig.Current.EntityFacade.LogContextManager.ValidateAsync(this, context);
            await ValidateAsync(context);
            return context.AllBusinessRules;
        }
        partial void OnAfterValidate(Sidvall.Data.BusinessRuleCollection itemBusinessRules, Sidvall.Business.ValidationContext context);
        public async System.Threading.Tasks.Task ValidateAsync(Sidvall.Business.ValidationContext context)
        {
            var itemBusinessRules = new Sidvall.Data.BusinessRuleCollection();
            OnAfterValidate(itemBusinessRules, context);
            await context.TaskContext.DoTaskAsync();
            context.AllBusinessRules.AddRange(context.Settings.IncludeNotBrokenRules, itemBusinessRules);
            var businessRulesProvider = this as Sidvall.Data.IBusinessRulesProvider;
            if (businessRulesProvider != null)
                businessRulesProvider.BusinessRules.AddRange(context.Settings.IncludeNotBrokenRules, itemBusinessRules);
        }

        #endregion
        #region Initialize

        public void Initialize()
        {
            global::Sidvall.LogManager.UI.SystemContextConfig.Current.EntityFacade.LogContextManager.Initialize(this);
        }
        public void Initialize(Sidvall.Data.InitializeSettings settings)
        {
            global::Sidvall.LogManager.UI.SystemContextConfig.Current.EntityFacade.LogContextManager.Initialize(this, settings);
        }

        #endregion
        
        #region ILogContext Members

        string global::Sidvall.LogManager.Data.ILogContext.Category
        {
            get
            {
                return this._Category;
            }
            set
            {
                this._Category = value;
            }
        }
        string global::Sidvall.LogManager.Data.ILogContext.Content
        {
            get
            {
                return this._Content;
            }
            set
            {
                this._Content = value;
            }
        }
        System.Nullable<global::Sidvall.Logging.EventType> global::Sidvall.LogManager.Data.ILogContext.EventTypeId
        {
            get
            {
                return this._EventTypeId;
            }
            set
            {
                this._EventTypeId = value;
            }
        }
        System.Nullable<int> global::Sidvall.LogManager.Data.ILogContext.LogContextId
        {
            get
            {
                return this._LogContextId;
            }
            set
            {
                this._LogContextId = value;
            }
        }

        #endregion

        #endregion
        #region Constructors

        public LogContext()
        {
        }

        public LogContext(Sidvall.Data.DataRowState? dataRowState)
            : base(dataRowState)
        {
        }

        #endregion
    }
}